# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# Copyright (C) 2012 Rami Dabain teamerx@gmail.com
# This program is free software: you can redistribute it and/or modify it 
# under the terms of the GNU General Public License version 3, as published 
# by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranties of 
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
# PURPOSE.  See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along 
# with this program.  If not, see <http://www.gnu.org/licenses/>.
### END LICENSE

import optparse
import httplib2, urllib,  urllib2
import gettext, json
import threading
import cookielib
from login import do_login
from netCheck import netCheck
from notify import notify
import hashlib, time
from uuid import getnode as get_mac
from gettext import gettext as _
gettext.textdomain('noc-client')

from gi.repository import Gtk, Gdk, GObject, Notify # pylint: disable=E0611

from noc_client import NocClientWindow


from noc_client_lib import set_up_logging, get_version
from configobj import ConfigObj
import fcntl, sys

GObject.threads_init()


def parse_options():
    """Support for command line options"""
    parser = optparse.OptionParser(version="%%prog %s" % get_version())
    parser.add_option(
        "-v", "--verbose", action="count", dest="verbose",
        help=_("Show debug messages (-vv debugs noc_client_lib also)"))
    (options, args) = parser.parse_args()

    set_up_logging(options)

def main():
    'constructor for your class instances'
    pid_file = 'program.pid'
    fp = open(pid_file, 'w')
    try:
        fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
    except IOError:
    # another instance is running
        print "exiting for instance"
        sys.exit(0)
    path = "file://"+ __file__ + "../data/media/alert.mp3"
    print path
    parse_options()

    # Run the application.    
    config = ConfigObj('config.dat')
    
    #import pyglet
    #sound = pyglet.media.load('mysound.mp3', streaming=False)
    #sound.play()
    #pyglet.app.run()
    print __file__
    Gtk.tray = ''
    window = NocClientWindow.NocClientWindow()

    is_logged = False
    
    Gtk.mainWindow = window
    Gtk.is_logged = False
    Gtk.session_id = ''
    Gtk.server = ''
    Gtk.servers = {}
    Gtk.lastServerId = ''
    
    
    m = hashlib.md5()
    m.update(str(get_mac()))
    
    Gtk.client_id = m.hexdigest()
    #print Gtk.client_id
    try:
        email = config['email']
    except:
        email = ''

    try:
        passw = config['passw']
    except:
        passw = ''
    
    if email == '':
        window.show()
    else:
        netCheck(do_login,email,passw)
        #window.show()
   
    
    Gdk.threads_enter()
    Gtk.main()
    Gdk.threads_leave()

               
#is_logged = False
#session_id = ''
#server = ''
#servers = {}
#mainWindow = ''

